﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreClientModule;
using uCoreClasses;

namespace MBBStoreClient
{
    public partial class frmContentManagement : Form
    {
        ProductContentManager pcm = new ProductContentManager();
        uThreader t = new uThreader();
        public frmContentManagement()
        {
            InitializeComponent();

            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);
            pcm.ProcessCompleted += new ModuleEventHandler(pcm_ProcessCompleted);
            pcm.ProcessError += new ModuleEventHandler(pcm_ProcessError);
            txtSearch.KeyPress += new KeyPressEventHandler(txtSearch_KeyPress);
            lstProducts.DoubleClick += new EventHandler(lstProducts_DoubleClick);
            btnSave.Click += new EventHandler(btnSave_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            ClearFields();
            ControlState(true);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            grpDetails.Enabled = false;
            ContentDetailType cdt;
            cdt.fBarcode = txtBarcode.Text;
            cdt.fUnit = txtUnit.Text;
            cdt.fQty = txtQty.Text;
            pcm.SaveDetails(cdt);
            
        }

        void lstProducts_DoubleClick(object sender, EventArgs e)
        {
            if (lstProducts.SelectedIndex != -1)
            {
                lstProducts.Enabled = false;
                txtSearch.Enabled = false;
                pcm.GetContentDetail(pcm.ProductTable.Rows[lstProducts.SelectedIndex]["fBarcode"].ToString());
            }
        }

        void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                pcm.GetProductTable(txtSearch.Text, chkUnits.Checked);
            }
        }

        void pcm_ProcessError(string procCode, string msg)
        {
           t.SafeCall(this, delegate
           {
               MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               switch (procCode)
               {
                   case "GetContentDetail":
                       ControlState(true);
                       break;
                   case "SaveDetails":
                       grpDetails.Enabled = true;
                       break;
               }
           });
        }
        void pcm_ProcessCompleted(string procCode, string msg)
        {
            t.SafeCall(this, delegate
            {
                switch (procCode)
                {
                    case "GetProductTable":
                        ControlState(true);
                        lstProducts.Items.Clear();
                        lblCount.Text = pcm.ProductTable.Rows.Count + " result(s)";
                        for (int i = 0; i < pcm.ProductTable.Rows.Count; i++)
                        {
                            lstProducts.Items.Add(pcm.ProductTable.Rows[i]["fDescription"].ToString());
                        }
                        break;
                    case "GetContentDetail":
                        ControlState(false);
                        txtBarcode.Text = pcm.ProductTable.Rows[lstProducts.SelectedIndex]["fBarcode"].ToString();
                        txtDescription.Text = lstProducts.SelectedItem.ToString();
                        txtUnit.Text = pcm.ContentDetail.fUnit;
                        txtQty.Text = pcm.ContentDetail.fQty;
                        break;
                    case "SaveDetails":
                        ControlState(true);
                        ClearFields();
                        break;
                }
            });
        }
        void ControlState(bool s)
        {
            lstProducts.Enabled = s;
            txtSearch.Enabled = s;
            grpDetails.Enabled = !s;
        }
        void ClearFields()
        {
            txtBarcode.Text = "";
            txtDescription.Text = "";
            txtUnit.SelectedIndex = -1;
            txtQty.Text = "";
        }









        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlForm.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlForm; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
            grpDetails.Enabled = false;
            pcm.GetProductTable("", chkUnits.Checked);
        }
    }
}
